clear
set more off

use  "..\2004\04373-0001-Data.dta"
gen year = 2004
local year = 2005
foreach b in "04596-0001-Data.dta" "21240-0001-Data.dta" "23782-0001-Data.dta" "26701-0001-Data.dta" "29621-0001-Data.dta" "32722-0001-Data.dta" "34481-0001-Data.dta" "34933-0001-Data.dta" "35509-0001-Data.dta" "2014-36361-0001-Data.dta"  {
	append using  ../`year'/`b'
	keep IREDUC2 NEWRACE2 EMPSTATY JBSTATR2 EDUCCAT2 AGE2 IRMARIT IRSEX CATAGE QUARTER  HER30USE HERYRTOT  PRDAYPYR ANLYRTOT HEREVER HELAST3 ANALREC ANALAGE HERSMOKE HERSNIFF ANALYFU ANLEVER OXYCONTN HERYFU ANALWT_C HERAGE ANALREC year
	replace year = `year' if year==.
	local year = `year'+1
}

label variable ANLEVER "used pain relievers nonmedically" 
gen heroin_30 = HER30USE >=1 &HER30USE <=30
replace heroin_30 =. if HER30USE >=97 | HER30USE ==85
label variable heroin_30 "have you ever used heroin in the past 30 days"

gen heroin_ever = HEREVER ==1 
replace heroin_ever =. if HEREVER >=94
label variable heroin_ever "have you ever used heroin"

gen pain_30 = ANALREC ==1 |ANALREC ==11
replace heroin_ever=. if ANALREC==85 | ANALREC ==97 | ANALREC ==98
label variable pain_30 "have you used pain medicine recreationally in the past 30 days"

gen pain_ever = ANLEVER ==1
replace pain_ever =. if ANLEVER >=97
label variable pain_ever "have you ever used pain medicine recreationally"

gen ever_smoke_heroin = HERSMOKE ==1 | HERSMOKE ==3
replace ever_smoke_heroin =. if HERSMOKE >=97
label variable ever_smoke_heroin "have you ever smoked heroin" 

gen ever_snort_heroin = HERSNIFF==1 |HERSNIFF ==3
replace ever_snort_heroin =. if HERSNIFF>=97
label variable ever_snort_heroin "have you ever snorted heroin"

gen oxy_ever = OXYCONTN ==1 |OXYCONTN ==3 
replace oxy_ever =. if OXYCONTN ==6 | OXYCONTN ==85 |OXYCONTN >91
label variable oxy_ever "ever ever used oxycontn recreationally"

gen Among_heroin_ever = heroin_ever ==1&pain_ever ==1 & ANALYFU - HERYFU <=0 & HERYFU <=2015 & ANALYFU <=2015 
replace Among_heroin_ever =. if ANLEVER >=97 | ANALYFU >=2015
label variable Among_heroin_ever "you started pain med in the same year or earlier than when you started heroin"

gen age_heroin = HERAGE
replace age_heroin =. if HERAGE == 985 | HERAGE >=994
label variable age_heroin "Age when individual used heroin for the first time"

gen age_pain = ANALAGE
replace age_pain = . if ANALAGE ==985 | ANALAGE >=994
label variable age_pain "Age when indivual abused pain medication for the first time"

gen heroin_12months = HELAST3 <= 2
replace heroin_12months = . if HELAST3 >= 94
label variable heroin_12months "used heroin within the past 12 months"

gen pain_12months = ANALREC ==1 | ANALREC ==2 | ANALREC ==8 | ANALREC ==11 |ANALREC == 12
replace pain_12months = . if ANALREC == 85 | ANALREC >= 97
label variable pain_12months "used pain reliever nonmedically in the last year"

gen total_heroin_yr = HERYRTOT
replace total_heroin_yr = . if HERYRTOT == 985 | HERYRTOT >=994
label variable total_heroin_yr "total days used heroin in the past year"

gen total_pain_yr = ANLYRTOT
replace total_pain_yr =. if ANLYRTOT == 985 | ANLYRTOT >=994
label variable total_pain_yr "# days used pain reliever past 12 months"

gen pain_nm12 = PRDAYPYR
replace pain_nm12 = . if PRDAYPYR ==985 | PRDAYPYR ==989 | PRDAYPYR >=994
label variable pain_12months "# days used non medical pain reliever past 12 months"

label define age 1 "respondent is 12 yrs old" 12 "respondent is 13 yrs old" 3 "respondent is 14 yrs old" 4 "respondent is 15 yrs old" 5 "respondent is 16 yers old" 6 "respondent is 17 yrs old" 7 "respondent is 18 yrs old" 8 "respondent is 19 yrs old" 9 "respondent is 20 yrs old" 10 "respondent is 21 yers old" 11 "respondent is btwn 22 to 23 yrs old" 12 "respondent is btwn 24 to 25 yrs old" 13 "respondent is btwn 26 to 29" 14 "respondent is btwn 30 to 34 yrs old" 15 "respondent is btwn 35 to 49 yrs old" 16 "respondent is btwn 50 to 64 yrs old" 17 "respondent is 65 yrs old plus"
label values AGE2 age

gen mari_status = IRMARIT
replace mari_status =. if IRMARIT == 99
label define mariage 1 "Married" 2 "Widowed" 3 "Divorced or Separated" 4 "Never married" 
label values mari_status mariage
label variable mari_status "maritial status"

gen educ = EDUCCAT2
label variable educ "education level"
label define learn 1 "Less than high school" 2 "High school grad" 3 "Some college" 4 "College grad" 5 "12 to 17 years old"
label values educ learn

gen wrk_status = JBSTATR2
label variable wrk_status "Work status in past week"
replace wrk_status = . if JBSTATR2 == 10 | JBSTATR2 == 11
label define wk 1 "Full-Time Job" 2 "Part Time Job" 3 "has job or volunteer worker,but did not work past wk" 4 "unemployed" 5 "disabled" 6 "keeping house full-time" 7 "in school/ training" 8 "retired"  9 "does not have a job, some other reason" 
label values wrk_status wk

gen employ_st = EMPSTATY
replace employ_st = 99 if EMPSTATY == 5
label variable employ_st "Employment Status"
label define card 1 "Employed FT" 2 "Employed PT" 3 "Unemployed" 4 "Other(incl. not in labor force)" 99 "12-17 yrs old"
label values employ_st card

gen race = NEWRACE2
label define compute 1 "NonHis White" 2 "NonHisp Black" 3 "NonHisp Native Am" 4 "NonHisp Native HI" 5 "NonHisp Asian" 6 "NonHisp more than one race" 7 "Hispanic"
label values race compute

gen school = IREDUC2
label variable school "school level"
label define phone 1 "Fifth grade or less" 2 "sixth grade" 3 "seventh grade" 4 "eigth grade" 5 "ninth grade" 6 "tenth grade" 7 "eleventh grade" 8 "twelth grade" 9 "freshman college" 10 "sophomore college" 11 "senior college/ grad school/ professional school" 
label values school phone

save "./nsduh_compiled.dta", replace


***THIS IS PRETTY GOOD ***



 





